package com.linewell.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linewell.commom.controller.BackendBaseController;
import com.linewell.system.model.SysNotice;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.pagemodel.ReturnInfo;
import com.linewell.system.service.ISysNoticeService;

/**
 * 系统公告控制器
 */
@Controller
@RequestMapping("/manage/sysNotice")
public class SysNoticeController extends BackendBaseController{
	
		private static final Log log = LogFactory.getLog(SysNoticeController.class);
		@Autowired
		private ISysNoticeService sysNoticeService;
		/**
		 * 跳转至 sysNotice页
		 */
		@RequestMapping(value = "/index")
		public String index(HttpServletRequest request) {
			return super.index("/WEB-INF/pages/system/sys_notice/sys_notice_list.jsp", request);
		}	
		
		/**
		 * 数据查询
		 */
		@ResponseBody
		@RequestMapping(value = "/dataGrid")
		public DataGrid dataGrid(@ModelAttribute SysNotice n,PageHelper ph,HttpServletRequest request) {
			ph.setSort("id");
			ph.setOrder("desc");
			DataGrid dg = sysNoticeService.datagrid(ph,n,getUserDepId(request));
			return dg;
		}
		
		/**
		 * 添加
		 */
		@RequestMapping(value="/add")
		public String add(Map<String, Object> map){
			map.put("saveUrl", "/manage/sysNotice/addSave");
			return "/WEB-INF/pages/system/sys_notice/sys_notice_au.jsp";
		}
		
		/**
		 * 添加保存
		 */
		@ResponseBody
		@RequestMapping(value = "/addSave")
		public ReturnInfo addSave(@Valid SysNotice sysNotice, BindingResult b, ReturnInfo result,HttpServletRequest request) {
			if (b.getErrorCount() > 0) { // 数据校验
				for (FieldError error : b.getFieldErrors()) {
					log.info(error.getField() + ":" + error.getDefaultMessage());
					result.setMsg(error.getField() + "" + error.getDefaultMessage());
				}
				result.setSuccess(1);
			} else {
				if (sysNotice != null) {
					try {
						sysNotice.setPubUserId(getLoginUserId(request));
						
						if(sysNotice.getSysDepartment() != null){
							if(sysNotice.getSysDepartment().getDepId() == 0){
								sysNotice.setSysDepartment(null);
							}
						}
						sysNoticeService.save(sysNotice);
					} catch (Exception e) {
						log.info("保存失败" + e.getMessage());
						result.setMsg("保存失败！");
						result.setSuccess(1);
					}
				}
			}
			return result;
		}
		
		/**
		 * 修改
		 */
		@RequestMapping(value = "/edit")
		public String edit(String id, Map<String, Object> map) {
			map.put("saveUrl", "/manage/sysNotice/editSave");
			if (id != null) {
				List<SysNotice> model = sysNoticeService.getUser(id);
				map.put("model", model.get(0));
			}
			return "/WEB-INF/pages/system/sys_notice/sys_notice_au.jsp";
		}

		/**
		 * 修改保存
		 */
		@ResponseBody
		@RequestMapping(value = "/editSave")
		public ReturnInfo editSave(@Valid SysNotice sysNotice, BindingResult b, ReturnInfo result) {

			if (b.getErrorCount() > 0) { // 数据校验
				log.info("数据校验出错！");
				for (FieldError error : b.getFieldErrors()) {
					log.info(error.getField() + ":" + error.getDefaultMessage());
					result.setMsg(error.getField() + "" + error.getDefaultMessage());
				}
				result.setSuccess(1);
			} else {
				if (sysNotice != null) {
					
					try {
						log.info(sysNotice.getSysDepartment().getDepId());
						if (sysNotice.getSysDepartment().getDepId() == 0) {
							sysNotice.setSysDepartment(null);
						}
						sysNoticeService.update(sysNotice);
					} catch (Exception e) {
						log.info("保存失败" + e.getMessage());
						result.setMsg("保存失败！");
						result.setSuccess(1);
					}
				}
			}
			return result;
		}
		
		/**
		 * 删除（物理删除）
		 */
		@ResponseBody
		@RequestMapping(value = "/delete")
		public ReturnInfo delete(String ids, ReturnInfo result) {

			if (ids != null) {
				try {
					sysNoticeService.deleteAll(ids);
				} catch (Exception e) {
					result.setMsg("删除失败！");
					result.setSuccess(1);
				}
			}

			return result;
		}

		/**
		 * 移除（逻辑删除）
		 */
		@ResponseBody
		@RequestMapping(value = "/remove")
		public ReturnInfo remove(String ids, ReturnInfo result) {
			if (ids != null) {
				try {
					sysNoticeService.removeAll(ids);
				} catch (Exception e) {
					log.info("移除异常" + e.getMessage());
					result.setMsg("移除失败！");
					result.setSuccess(1);
				}
			}
			return result;
		}
}